Skip to content

Comments

fix: parse IP address without the port number#3865

Merged
charn merged 1 commit intomainfrom
resilient-logger
Feb 3, 2026
Merged

fix: parse IP address without the port number#3865
charn merged 1 commit intomainfrom
resilient-logger

Conversation

@charn
Copy link
Contributor

@charn charn commented Feb 2, 2026

IP address type doesn't accept port numbers in elastic.

Refs: HL-1614

IP address type doesn't accept port numbers in elastic.

Refs: HL-1614
@charn charn requested review from a team as code owners February 2, 2026 16:17
Comment on lines +12 to +15
client_ip = client_ip.lstrip("[").split("]")[0]
elif "." in client_ip and client_ip.count(":") == 1:
# IPv4 with port
client_ip = client_ip.split(":")[0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The port-stripping logic fails for unbracketed IPv4-mapped IPv6 addresses (e.g., ::ffff:192.0.2.1:8000) because it incorrectly checks for a single colon, leaving the port attached.
Severity: MEDIUM

Suggested Fix

Update the logic to correctly identify and strip ports from unbracketed IPv6 addresses. This could involve checking for a dot and multiple colons, then finding the last colon to reliably split the host from the port, regardless of the IP address format.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: backend/shared/shared/audit_log/utils.py#L12-L15

Potential issue: The logic to strip ports from IP addresses does not correctly handle
unbracketed IPv4-mapped IPv6 addresses that contain a port, such as
`::ffff:192.0.2.1:8000`. The condition `elif "." in client_ip and client_ip.count(":")
== 1` fails because such addresses contain multiple colons. As a result, the port is not
stripped. This causes an IP address with a port to be sent to Elasticsearch, which
cannot index it as an `ip` type, leading to the silent failure of audit log persistence
and data loss.

Did we get this right? 👍 / 👎 to inform future reviews.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 2, 2026

@azure-pipelines
Copy link

YJDH-KESASETELI-API branch is deployed to platta: https://yjdh-kesaseteli-pr3865.api.dev.hel.ninja 🚀🚀🚀

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 2, 2026

@azure-pipelines
Copy link

YJDH-HELSINKILISA-API branch is deployed to platta: https://helsinkilisa-pr3865.api.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

YOUTH branch is deployed to platta: https://nuortenkesaseteli-pr3865.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

HANDLER branch is deployed to platta: https://kesaseteli-handler-ui-pr3865.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

EMPLOYER branch is deployed to platta: https://kesaseteli-pr3865.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

TestCafe result is success for https://kesaseteli-handler-ui-pr3865.dev.hel.ninja 😆🎉🎉🎉

@azure-pipelines
Copy link

TestCafe result is success for https://nuortenkesaseteli-pr3865.dev.hel.ninja 😆🎉🎉🎉

@azure-pipelines
Copy link

APPLICANT is deployed to platta: https://helsinkilisa-ui-pr3865.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

TestCafe result is success for https://kesaseteli-pr3865.dev.hel.ninja 😆🎉🎉🎉

@azure-pipelines
Copy link

HANDLER branch is deployed to platta: https://helsinkilisa-ui-handler-pr3865.dev.hel.ninja 🚀🚀🚀

@azure-pipelines
Copy link

TestCafe result is success for https://helsinkilisa-ui-pr3865.dev.hel.ninja 😆🎉🎉🎉

@azure-pipelines
Copy link

TestCafe result is success for https://helsinkilisa-ui-handler-pr3865.dev.hel.ninja 😆🎉🎉🎉

Copy link
Collaborator

@karisal-anders karisal-anders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that part of get_remote_address function could've been replaced by using ipaddress.ip_address but it doesn't support ports, e.g.:

>>> from ipaddress import ip_address
>>> ip_address("192.168.0.1:1234")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Python312/Lib/ipaddress.py", line 54, in ip_address
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
ValueError: '192.168.0.1:1234' does not appear to be an IPv4 or IPv6 address

Approval from Kesäseteli side for the shared code part.

@charn charn merged commit e769fdd into main Feb 3, 2026
131 checks passed
@charn charn deleted the resilient-logger branch February 3, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants